<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	分销插件所有金额字段均保存整形，精确到分。显示时，除以100然后保留两位小数即可
	佣金比例字段保存整形，值为万分数，相当于百分数的精确到小数点后2位。即展示到前端时，需要除以100，换算成百分数显示
*/
class member extends \plugin{
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('会员注册失败，缺少会员ID！');
		}
		$user=pdo_get('jy_weishop_member',array('merchid'=>$member['merchid']?:0,'mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
		if($user['id']){
			return false;
		}
		$weishop_member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['mid']),'fromuser');
		$fromuser=$weishop_member['fromuser'];
		$fromuser=$fromuser?:$_SESSION['fromuser'];
		$member['fromuser']=$fromuser;
		if($fromuser){
			$member['workerid']=pdo_getcolumn('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser),'id');
		}
		$this->insert('member',$member);
		//新人发券
		$coupon=$this->pdo_get('coupon_sendticket',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>0));
		if($coupon){
			$limittime=json_decode($coupon['limittime'],true);
			if($limittime['tstart']<TIMESTAMP && $limittime['tend']>TIMESTAMP){
				$params=array(
					'mid'=>$member['mid'],
					'ac_type'=>'new',
					'ac_id'=>$coupon['id'],
					'remark'=>'新人发券活动',
				);
				$this->model('coupon')->give_coupon($params);
			}
		}
		//新人参与游戏
		$game=$this->pdo_get('game',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>3));
		if($game){
				$params=array(
					'mid'=>$member['mid'],
					'game_id'=>$game['id'],
					'remark'=>'购物参与活动',
				);
				$this->model('game')->add_game($params);
		}
		//新人奖励
		$this->model('activity/sales')->bindmobile_credit();
		$this->model('activity/sales')->upyao_fee($member['mid'],true);
		return '';
	}
	public function checklogin($storeid='',$area_agentid='',$area_code='',$merchid=0,$isres=false){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json']?:'jy_weishop/index/';
			if($_SESSION['os']=='windows'){
				if($_SESSION['show_lang']=='en'){
					exi('Please login first！','error','jy_weishop/member.login/');
				}else{
					exi('请先登陆！','error','jy_weishop/member.login/');
				}
			}else{
				if($isres){
					$res=model('core/core')->checklogin('','',true);
					return $res;
				}else{
					model('core/core')->checklogin();
					return '';
				}
			}
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if(empty($member)){
				model('core/member')->logout();
				if($isres){
					$res=model('core/core')->checklogin('','',true);
					return $res;
				}else{
					model('core/core')->checklogin();
					return '';
				}
			}else{
				$_SESSION['checklogin']=TIMESTAMP;
				$user=pdo_get('jy_weishop_member',array('merchid'=>$merchid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'uniacid'=>$_SESSION['uniacid'],
						'storeid'=>$storeid?:$_SESSION['storeid'],
						'merchid'=>$merchid,
						'area_agentid'=>$area_agentid?:$_SESSION['area_agentid'],
						'area_code'=>$area_code?:$_SESSION['area_code'],
						'createtime'=>TIMESTAMP,
					);
					$this->register($insert);
				}elseif(!$user['merchid'] && $merchid){
					pdo_update('jy_weishop_member',array('merchid'=>$merchid),array('id'=>$user['id'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		return '';
	}
	public function get_level($merchid=0,$mid=0){
		$mid=$mid?$mid:$_SESSION['uid'];
		$merchid=$merchid?:0;
		$levelid = pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'merchid'=>$merchid),'level');
		$levelid=$levelid?:0;
		if($levelid){
			$level=pdo_get('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$levelid),array('title','id','discount'));
		}
		$leveltitle = $level['title'];
		$leveltitle=$leveltitle?:'默认等级';
		$info=array('id'=>$levelid,'title'=>$leveltitle,'discount'=>$level['discount']);
		return $info;
	}
	public function get_group(){
		$groupid = pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'groupid');
		$grouptitle = pdo_getcolumn('jy_weishop_member_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$groupid),'title');
		$info=array('id'=>$groupid,'title'=>$grouptitle);
		return $info;
	}
	/*
		会员等级变化
	*/
	public function get_next_level($mid=0,$isall=false,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$current_level=$this->pdo_getcolumn('member',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'mid'=>$mid),'level');
		$member_level=$this->pdo_getcolumn('member_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$current_level),'level');
		$params=array(
			'params'=>array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'isup'=>1,'level >'=>$member_level),
			'orderby'=>'level asc',
		);
		$member_levels=$this->pdo_getlist('member_level',$params,$total);		
		if(!$member_levels){
			return array();
		}
		if($isall){
			return $member_levels;
		}
		return $member_levels[0];
	}
	public function up_member_level($mid=0,$level_id=0,$merchid=0,$type=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->pdo_update('member',array('level'=>$level_id),array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'level_id'=>$level_id,
			'type'=>$type,
			'ordersn'=>get_ordersn('jy_weishop_member_viplog','ordersn'),
			'ispay'=>0,
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_member_viplog',$insert);
		$this->model('activity/sales')->upyao_fee($mid,true);
		$this->up_member_levelcoupons($mid,$level_id);
		return true;
	}
	public function up_member_levelcoupons($mid=0,$level_id=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$level=pdo_get('jy_weishop_member_level',array('id'=>$level_id,'uniacid'=>$_SESSION['uniacid']));
		if($level['fan_fee']>0){
			if($merchmid){
				model('core/member')->member_credit($merchmid,'credit2',$level['fan_fee'],true,'jy_weishop','会员升级返现');
			}else{
				model('core/member')->member_credit($mid,'credit2',$level['fan_fee'],true,'jy_weishop','会员升级返现');
			}
		}
		$couponids=str_array($level['couponids']);
		foreach($couponids as $couponid){
			if(!$couponid || !$mid){
				continue;
			}
			$this->model('coupon')->getcoupon($couponid,$mid,true,true,$merchid);
		}
		if($level['up_type']==4){
			
		}
		return true;
	}
	public function up_member_openfee($mid=0,$level_id=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$level=pdo_get('jy_weishop_member_level',array('id'=>$level_id,'uniacid'=>$_SESSION['uniacid']));
		if($level['up_type']==3 && $level['open_fee']>0){
			if($merchmid){
				model('core/member')->member_credit($merchmid,'credit2',$level['open_fee'],true,'jy_weishop','充值Vip');
			}else{
				model('core/member')->member_credit($mid,'credit2',$level['open_fee'],true,'jy_weishop','充值Vip');
			}
		}elseif($level['up_type']==4 && $level['open_fee']>0){
			pdo_update('jy_weishop_member',array('lastfanfee +='=>$level['open_fee']),array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function vip_fanfee($mid=0,$price=0,$merchid=0){
		if($price>0){
			$member=pdo_get('jy_weishop_member',array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('lastfanfee','level'));
			$level=pdo_get('jy_weishop_member_level',array('id'=>$member['level'],'uniacid'=>$_SESSION['uniacid']),array('up_type','open_fanrate'));
		}
		if($member['lastfanfee']>0 && $level['open_fanrate']>0 && $level['up_type']==4){
			$fanfee=$level['open_fanrate']*$price/100;
			$fanfee=$fanfee>$member['lastfanfee']?$member['lastfanfee']:$fanfee;
			model('core/member')->member_credit($mid,'credit2',$fanfee,true,'jy_weishop','充值Vip购物返');
			pdo_update('jy_weishop_member',array('lastfanfee -='=>$fanfee),array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function check_uplevel($mid=0,$up_type=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member_levels=$this->get_next_level($mid,true,$merchid);
		//高等级匹配开始
		paixu($member_levels,'level','desc');
		foreach($member_levels as $item){
			if($item['up_type']==2){
				continue;
			}
			$isup=true;
			if($item['id'] && $item['up_type']==$up_type){
				//升级会员等级
				if($up_type==1){
					$ordercount=$this->pdo_count('order',array('mid'=>$mid,'ispay'=>1,'merchid'=>$merchid),'id');
					$ordermoney=$this->pdo_getsum('order',array('mid'=>$mid,'ispay'=>1,'merchid'=>$merchid),'price');
				}elseif($up_type==0){
					$ordercount=$this->pdo_count('order',array('mid'=>$mid,'status'=>3,'merchid'=>$merchid),'id');
					$ordermoney=$this->pdo_getsum('order',array('mid'=>$mid,'status'=>3,'merchid'=>$merchid),'price');
				}
				$ordermoney=$ordermoney?:0;
				if($ordercount<$item['ordercount'] && $item['ordercount']){
					$isup=false;
				}
				if($ordermoney<$item['ordermoney'] && $item['ordermoney']){
					$isup=false;
				}
				if($item['goods_ids']){
					$goods_ids=json_decode($item['goods_ids'],true);
					$lastbuytime=time()-24*3600;
					$isgoods=false;
					if(count($goods_ids)){
						foreach($goods_ids as $gv){
							$isgoods=true;
							$isbuy=$this->pdo_count('order_goods',array('mid'=>$mid,'goodsid'=>$gv['id'],'ispay'=>1,'createtime >'=>$lastbuytime,'merchid'=>$merchid),'id');
							if($isbuy>0){
								break;
							}
						}
						if(!$isbuy){
							$isup=false;
						}
					}
				}
				if($isup){
					$this->up_member_level($mid,$item['id'],$merchid,1);
					break;
				}
			}
		}
		return true;
	}
	public function get_nextlevel($mid=0,$isup=false,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->pdo_get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('level','viptime'));
		$levelid=$member['level'];
		$mylevel=pdo_get('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']));
		$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'level >'=>$mylevel['level'],'status'=>1);
		$params['isup OR']='isup=1 OR up_type>=2';
		$nextlevel=pdo_get('jy_weishop_member_level',$params,'','level asc');
		if(!$nextlevel && $member['viptime'] && $member['viptime']<time()){
			$nextlevel=$mylevel;
			$nextlevel['isxufee']=1;
		}
		$nextlevel['nowtitle']=$mylevel['title'];
		$nextlevel['nowcardbg']=$mylevel['cardbg'];
		if(!$nextlevel['id']){
			return $nextlevel;
		}
		if($isup){
			$nextlevel['couponids']=str_array($nextlevel['couponids']);
			$nextlevel['sheng_fees']=$nextlevel['sheng_fees']?json_decode($nextlevel['sheng_fees'],true):array();
			$nextlevel['jie_fee']=0;
			foreach($nextlevel['sheng_fees'] as $sv){
				$nextlevel['jie_fee'] +=$sv['fee'];
			}
			$nextlevel['coupon_fee']=0;
			foreach($nextlevel['couponids'] as $sv){
				$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$sv),array('id','title','deduct'));
				$nextlevel['coupon_fee'] +=$coupon['deduct'];
			}
		}
		return $nextlevel;
	}
	public function get_levelinfo($levelid=0,$isup=false){
		$levelinfo=pdo_get('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']));
		$levelinfo['couponids']=str_array($levelinfo['couponids']);
		$levelinfo['sheng_fees']=$levelinfo['sheng_fees']?json_decode($levelinfo['sheng_fees'],true):array();
		$levelinfo['jie_fee']=0;
		foreach($levelinfo['sheng_fees'] as $sv){
				$levelinfo['jie_fee'] +=$sv['fee'];
		}
		$levelinfo['coupon_fee']=0;
		foreach($levelinfo['couponids'] as $sv){
				$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$sv),array('id','title','deduct'));
				$levelinfo['coupon_fee'] +=$coupon['deduct'];
		}
		return $levelinfo;
	}
}